package com.vhly.remoteapi.yahoo.contact.field;

import android.util.Log;
import com.vhly.remoteapi.yahoo.contact.FieldValue;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-5
 * Time: 17:12:52
 * Email: vhly@163.com
 */
public class Name extends FieldValue {

    private String name;
    private String givenName;
    private String middleName;
    private String familyName;
    private String prefix;
    private String suffix;
    private String givenNameSound;
    private String familyNameSound;

    public Name(String n) {
        name = n;
    }

    /**
     * read object from DataInput
     *
     * @param din DataInput
     */
    @Override
    public void readObject(DataInput din) throws IOException {
        byte b = din.readByte();
        if (b == 1) {
            givenName = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            middleName = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            familyName = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            prefix = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            suffix = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            givenNameSound = din.readUTF();
        }
        b = din.readByte();
        if (b == 1) {
            familyNameSound = din.readUTF();
        }
    }

    /**
     * Write object to DataOutput
     *
     * @param dout DataOutput
     */
    @Override
    public void writeObject(DataOutput dout) throws IOException {
        if (givenName != null) {
            dout.writeByte(1);
            dout.writeUTF(givenName);
        } else {
            dout.writeByte(0);
        }
        if (middleName != null) {
            dout.writeByte(1);
            dout.writeUTF(middleName);
        } else {
            dout.writeByte(0);
        }
        if (familyName != null) {
            dout.writeByte(1);
            dout.writeUTF(familyName);
        } else {
            dout.writeByte(0);
        }
        if (prefix != null) {
            dout.writeByte(1);
            dout.writeUTF(prefix);
        } else {
            dout.writeByte(0);
        }
        if (suffix != null) {
            dout.writeByte(1);
            dout.writeUTF(suffix);
        } else {
            dout.writeByte(0);
        }
        if (givenNameSound != null) {
            dout.writeByte(1);
            dout.writeUTF(givenNameSound);
        } else {
            dout.writeByte(0);
        }
        if (familyNameSound != null) {
            dout.writeByte(1);
            dout.writeUTF(familyNameSound);
        } else {
            dout.writeByte(0);
        }
    }


    /**
     * Serial content to XML
     *
     * @return String
     */
    @Override
    public String toXML() {
        String ret;
        StringBuffer sb = new StringBuffer();
        if (givenName != null) {
            sb.append("<givenName>").append(givenName).append("</givenName>");
        }
        if (middleName != null) {
            sb.append("<middleName>").append(middleName).append("</middleName>");
        }
        if (familyName != null) {
            sb.append("<familyName>").append(familyName).append("</familyName>");
        }
        if (prefix != null) {
            sb.append("<prefix>").append(prefix).append("</prefix>");
        }
        if (suffix != null) {
            sb.append("<suffix>").append(suffix).append("</suffix>");
        }
        if (givenNameSound != null) {
            sb.append("<givenNameSound>").append(givenNameSound).append("</givenNameSound>");
        }
        if (familyNameSound != null) {
            sb.append("<familyNameSound>").append(familyNameSound).append("</familyNameSound>");
        }
        ret = sb.toString();
        sb = null;
        return ret;
    }

    /**
     * Parse Element
     *
     * @param el Element
     */
    @Override
    public void parse(Element el) {
        if (el != null) {
            NodeList nlist = el.getElementsByTagName("givenName");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        givenName = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("middleName");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        middleName = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("familyName");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        familyName = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("prefix");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        prefix = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("suffix");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        suffix = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("givenNameSound");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        givenNameSound = ((Text) txt).getNodeValue();
                    }
                }
            }

            nlist = el.getElementsByTagName("familyNameSound");
            if (nlist != null && nlist.getLength() > 0) {
                Element gne = (Element) nlist.item(0);
                Node txt = gne.getFirstChild();
                if (txt != null) {
                    if (txt instanceof Text) {
                        familyNameSound = ((Text) txt).getNodeValue();
                    }
                }
            }
        }
    }

    @Override
    public String toString() {
        String ret = null;
        if (name == null) {
            StringBuffer sb = new StringBuffer();
            Locale locale = Locale.getDefault();
            String country = locale.getCountry();
            Log.d("Name", "Locale get Contry is " + country);
            if (country.toLowerCase().equals("cn")) {
                // TODO FamilyName first
                if (familyName != null) {
                    sb.append(familyName);
                }
                if (givenName != null) {
                    sb.append(givenName).append(' ');
                }
                if (middleName != null) {
                    sb.append(middleName).append(' ');
                }

            } else {
                // TODO Check any contries name set.
                if (givenName != null) {
                    sb.append(givenName).append(' ');
                }
                if (middleName != null) {
                    sb.append(middleName).append(' ');
                }
                if (familyName != null) {
                    sb.append(familyName);
                }
            }
            name = sb.toString();
            sb = null;
            ret = name;
        }
        return ret;
    }
}
